package com.chinasoftosg.sicap.dao.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.chinasoftosg.sicap.dao.ILoginDao;
import com.chinasoftosg.sicap.pojo.AdminInfo;
import com.chinasoftosg.sicap.pojo.Rights;
import com.chinasoftosg.sicap.pojo.mapper.AdminInfoMapper;
import com.chinasoftosg.sicap.pojo.mapper.RightsMapper;

public class LoginDaoImpl implements ILoginDao
{
    
    private JdbcTemplate jdbcTemplate;
    
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public AdminInfo login(String loginName, String loginPwd)
    {
        String sql = "select * from T_ADMININFO t where t.admin_name = ? and t.admin_password = ?";
        try
        {
            AdminInfo adminInfo = this.jdbcTemplate.queryForObject(sql,
                    new AdminInfoMapper(),
                    new Object[] {loginName, loginPwd});
            return adminInfo;
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public List<Rights> getRightsList(int roleId)
    {
        String sql = "select t1.* from T_RIGHTS t1 ,T_ROLE_RIGHTS t2,T_ROLE t3 where t3.role_id = t2.role_id and t2.rights_id = t1.rights_id and t3.role_id = ? order by t1.rights_no";
        try
        {
            List<Rights> list = this.jdbcTemplate.query(sql,
                    new RightsMapper(),
                    new Object[] {roleId});
            
            return list;
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    public void d()
    {
        DriverManagerDataSource dataSource = null;
        try
        {
            dataSource = new DriverManagerDataSource(
                    "oracle.jdbc.driver.OracleDriver",
                    "jdbc:oracle:thin:@192.168.124.134:1521:ORCL",
                    "umb_gmli_0426", "umb_gmli_0426");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
                int num = jdbcTemplate.queryForInt("select count(*) from T_ADMININFO where 1=11");
                System.out.println(num);
        //        List<AdminInfo> adminInfos = jdbcTemplate.query("select * from T_ADMININFO where 1=1",
        //                new AdminInfoMapper());
        //        System.out.println(adminInfos);
        //jdbcTemplate.queryForList("select * from T_ADMININFO", new AdminInfoMapper());
        // String sql = "select admin_Id,Admin_Name,Admin_Password,Role_Id from T_ADMININFO where ADMIN_ID = 1";
        // AdminInfo adminInfo = jdbcTemplate.queryForObject(sql, new AdminInfoMapper());
        // System.out.println(adminInfos.get(0).getAdminName());
    }
    
    public static void main(String[] args)
    {
        LoginDaoImpl daoImpl = new LoginDaoImpl();
        daoImpl.d();
    }

    @Override
    public List<Rights> getRightsListAll()
    {
        String sql = "select t1.* from T_RIGHTS t1 where t1.rights_status = 1 order by t1.rights_no";
        try
        {
            List<Rights> list = this.jdbcTemplate.query(sql,
                    new RightsMapper());
            return list;
        }
        catch (DataAccessException e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
